package com.example.doer.reading.AppActivities;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.ab.activity.AbActivity;
import com.ab.http.AbHttpUtil;
import com.ab.http.AbStringHttpResponseListener;
import com.ab.util.AbDialogUtil;
import com.ab.util.AbFileUtil;
import com.ab.util.AbImageUtil;
import com.ab.util.AbLogUtil;
import com.ab.util.AbStrUtil;
import com.ab.util.AbToastUtil;
import com.example.doer.reading.R;
import com.example.doer.reading.global.MyApplication;

import java.io.File;

public class testActivity extends AbActivity {
    /* 用来标识请求照相功能的activity */
    private static final int CAMERA_WITH_DATA = 3023;
    /* 用来标识请求gallery的activity */
    private static final int PHOTO_PICKED_WITH_DATA = 3021;
    /* 用来标识请求裁剪图片后的activity */
    private static final int CAMERA_CROP_DATA = 3022;

    /* 拍照的照片存储位置 */
    private File PHOTO_DIR = null;
    // 照相机拍照得到的图片
    private File mCurrentPhotoFile;
    private String mFileName;
    private View mAvatarView = null;
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.activity_test);



        final String url="http://192.168.191.1/Reading/TXTDoServlet?Action=download&BookID=0001";

        final AbHttpUtil abHttpUtil=AbHttpUtil.getInstance(this);

        Button button=(Button)findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivity(new Intent(testActivity.this,test2Activity.class));

//                AbFileUtil.downloadFile(url,Environment.getExternalStorageState()+"/ReadingDoer");

//                abHttpUtil.get(url, new AbStringHttpResponseListener() {
//                    @Override
//                    public void onSuccess(int i, String s) {
//                        Log.e("TXT",s);
//                    }
//
//                    @Override
//                    public void onStart() {
//
//                    }
//
//                    @Override
//                    public void onFinish() {
//
//                    }
//
//                    @Override
//                    public void onFailure(int i, String s, Throwable throwable) {
//
//                    }
//                });
            }
        });

        imageView= (ImageView) findViewById(R.id.result_photo);

        Button button_photo=(Button)findViewById(R.id.test);
        button_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAvatarView = mInflater.inflate(R.layout.choose_avatar, null);
                Button albumButton = (Button)mAvatarView.findViewById(R.id.choose_album);
                Button camButton = (Button)mAvatarView.findViewById(R.id.choose_cam);
                Button cancelButton = (Button)mAvatarView.findViewById(R.id.choose_cancel);
                albumButton.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        AbDialogUtil.removeDialog(testActivity.this);
                        // 从相册中去获取
                        try {
                            Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
                            intent.setType("image/*");
                            startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
                        } catch (ActivityNotFoundException e) {
                            AbToastUtil.showToast(testActivity.this,"没有找到照片");
                        }
                    }

                });

                camButton.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        AbDialogUtil.removeDialog(testActivity.this);
                        doPickPhotoAction();
                    }

                });

                cancelButton.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        AbDialogUtil.removeDialog(testActivity.this);
                    }

                });
                AbDialogUtil.showDialog(mAvatarView, Gravity.BOTTOM);
            }
        });

    }

    /**
     * 从照相机获取
     */
    private void doPickPhotoAction() {
        String status = Environment.getExternalStorageState();
        //判断是否有SD卡,如果有sd卡存入sd卡在说，没有sd卡直接转换为图片
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            doTakePhoto();
        } else {
            AbToastUtil.showToast(testActivity.this,"没有可用的存储卡");
        }
    }
    /**
     * 拍照获取图片
     */
    protected void doTakePhoto() {
        try {
            mFileName = System.currentTimeMillis() + ".jpg";
            mCurrentPhotoFile = new File(PHOTO_DIR, mFileName);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhotoFile));
            startActivityForResult(intent, CAMERA_WITH_DATA);
        } catch (Exception e) {
            AbToastUtil.showToast(testActivity.this,"未找到系统相机程序");
        }
    }

    /**
     * 描述：因为调用了Camera和Gally所以要判断他们各自的返回情况,
     * 他们启动时是这样的startActivityForResult
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent mIntent) {
        if (resultCode != RESULT_OK){
            return;
        }
        switch (requestCode) {
            case PHOTO_PICKED_WITH_DATA:
                Uri uri = mIntent.getData();
                String currentFilePath = getPath(uri);
                if(!AbStrUtil.isEmpty(currentFilePath)){
                    Intent intent1 = new Intent(this, CropImageActivity.class);
                    intent1.putExtra("PATH", currentFilePath);
                    startActivityForResult(intent1, CAMERA_CROP_DATA);
                }else{
                    AbToastUtil.showToast(testActivity.this,"未在存储卡中找到这个文件");
                }
                break;
            case CAMERA_WITH_DATA:
                AbLogUtil.d(testActivity.class, "将要进行裁剪的图片的路径是 = " + mCurrentPhotoFile.getPath());
                String currentFilePath2 = mCurrentPhotoFile.getPath();
                Intent intent2 = new Intent(this, CropImageActivity.class);
                intent2.putExtra("PATH",currentFilePath2);
                startActivityForResult(intent2,CAMERA_CROP_DATA);
                break;
            case CAMERA_CROP_DATA:
                String path = mIntent.getStringExtra("PATH");
                AbLogUtil.d(testActivity.class, "裁剪后得到的图片的路径是 = " + path);
                //Bitmap bitmap = AbImageBaseCache.getInstance().getBitmap(path);
                Bitmap mBitmap = AbFileUtil.getBitmapFromSD(new File(path), AbImageUtil.SCALEIMG, 116,116);
                imageView.setImageBitmap(mBitmap);
                String upURL="http://192.168.191.1/Reading/ImageDoServlet?Action=upload&Category=UserLogo&ImageName="+ MyApplication.user.getUserID();
                AbHttpUtil.getInstance(this).get(upURL, new AbStringHttpResponseListener() {
                    @Override
                    public void onSuccess(int i, String s) {

                    }

                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void onFailure(int i, String s, Throwable throwable) {

                    }
                });
//               mImagePathAdapter.addItem(mImagePathAdapter.getCount()-1,path);
//                camIndex++;
                break;
        }
    }

    /**
     * 从相册得到的url转换为SD卡中图片路径
     */
    public String getPath(Uri uri) {
        if(AbStrUtil.isEmpty(uri.getAuthority())){
            return null;
        }
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String path = cursor.getString(column_index);
        return path;
    }










    protected void onDestroy() {
        super.onDestroy();
    }



}
